package com.pig4cloud.pig.module.controller.raffle;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.raffle.RaffleGoods;
import com.pig4cloud.pig.module.api.entity.raffle.RaffleUserRecord;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.raffle.IRaffleGoodsService;
import com.pig4cloud.pig.module.service.raffle.IRaffleUserRecordService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 抽奖记录Controller
 *
 * @author ruoyi
 * @date 2021-08-29
 */
@Validated
@Api(value = "抽奖记录控制器", tags = {"抽奖记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/raffle/record")
public class RaffleUserRecordController {

    private final IRaffleUserRecordService raffleUserRecordService;
	private final IRaffleGoodsService raffleGoodsService;
	private final IWxUserService wxUserService;
	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

    /**
     * 查询抽奖记录列表
     */
    @ApiOperation("查询抽奖记录列表")
    @PreAuthorize("@pms.hasPermission('module:raffleRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<RaffleUserRecord> list(@Validated RaffleUserRecord raffleUserRecord) {
		QueryWrapper<RaffleUserRecord> qw = new QueryWrapper<>();
		if(raffleUserRecord.getRaffleId()!=null){
			qw.eq("raffle_id",raffleUserRecord.getRaffleId());
		}
		TableDataInfo<RaffleUserRecord> td = PageUtils.buildDataInfo(raffleUserRecordService.page(PageUtils.buildPage(),qw));
		List<RaffleUserRecord> list = td.getData();
		if(CollUtil.isNotEmpty(list)){
			for (RaffleUserRecord record : list){
				RaffleGoods goods = raffleGoodsService.findById(record.getGoodsId());
				if(goods!=null){
					record.setGoodsName(goods.getRaffleGoodsName());
				}
				WxUser wxUser = wxUserService.findById(record.getUserId());
				if(wxUser!=null){
					if(StrUtil.isNotEmpty(wxUser.getNickName())){
						record.setNickName(EmojiParser.parseToUnicode(wxUser.getNickName()));
					}
					record.setFaceUrl(wxUser.getFaceUrl());
					if(StrUtil.isNotEmpty(wxUser.getFaceUrl()) && wxUser.getFaceUrl().indexOf("https") < 0) {
						record.setFaceUrl(imgUrl + wxUser.getFaceUrl());
					}
				}
			}
		}
        return td;
    }

    /**
     * 导出抽奖记录列表
     */
    @ApiOperation("导出抽奖记录列表")
    @PreAuthorize("@pms.hasPermission('module:raffleRecord:export')")
    @SysLog("抽奖记录")
    @GetMapping("/export")
    public List export(@Validated RaffleUserRecord raffleUserRecord) {
        List<RaffleUserRecord> list = raffleUserRecordService.list();
        // ExcelUtil<RaffleUserRecord> util = new // ExcelUtil<RaffleUserRecord>(RaffleUserRecord.class);
        return  list;// util.exportExcel(list, "抽奖记录");
    }

    /**
     * 获取抽奖记录详细信息
     */
    @ApiOperation("获取抽奖记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:raffleRecord:query')")
    @GetMapping("/{id}")
    public R<RaffleUserRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        RaffleUserRecord raffleUserRecord = raffleUserRecordService.getById(id);
        return R.ok(raffleUserRecord);
    }

    /**
     * 新增抽奖记录
     */
    @ApiOperation("新增抽奖记录")
    @PreAuthorize("@pms.hasPermission('module:raffleRecord:add')")
    @SysLog("抽奖记录")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody RaffleUserRecord raffleUserRecord) {
        return R.okByRow(raffleUserRecordService.save(raffleUserRecord) ? 1 : 0);
    }

    /**
     * 修改抽奖记录
     */
    @ApiOperation("修改抽奖记录")
    @PreAuthorize("@pms.hasPermission('module:raffleRecord:edit')")
    @SysLog("抽奖记录")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody RaffleUserRecord raffleUserRecord) {
        return R.okByRow(raffleUserRecordService.updateById(raffleUserRecord) ? 1 : 0);
    }

    /**
     * 删除抽奖记录
     */
    @ApiOperation("删除抽奖记录")
    @PreAuthorize("@pms.hasPermission('module:raffleRecord:remove')")
    @SysLog("抽奖记录")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(raffleUserRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
